package network;

import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;


public class Group implements Serializable, Comparable <Group>, Iterable<Buddy>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8623000861026187581L;
	private final String name;
	private LinkedList<Buddy> buddyMembers;

	public Group(String name) {
		buddyMembers = new LinkedList<Buddy>();
		this.name=name;
	}

	public Group(String name, LinkedList <Buddy> list) {
		this.name = name;
		this.buddyMembers = list;
	}

	public void addBuddy(Buddy b){
		buddyMembers.add(b);
	}

	public boolean contains(Buddy g){
		return buddyMembers.contains(g);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null || !(obj instanceof Group))
			return false;

		Group other = (Group) obj;
		if (this.name == other.name)
			return true;
		if(this.name == null || other.name == null)
			return false;
		else
			return name.equals(other.name);

	}
	@Override
	public int compareTo(Group o) {
		return this.name.compareTo(o.name);
	}

	/**
	 * @return
	 */
	public String getName() {
		return name;
	}

	public LinkedList<Buddy> getBuddyMembers() {
		return buddyMembers;
	}



	public void removeBudyinGroup(String name){
		Buddy b = new Buddy(name, null);
		buddyMembers.remove(b);
	}

	public void updateBuddyInGroup(Buddy b){
		int index = buddyMembers.indexOf(b);
		if (index != -1)
			buddyMembers.set(index, b);
	}

	public String toString() {
		return name;
	}

	@Override
	public Iterator<Buddy> iterator() {
		return buddyMembers.iterator();
	}

}
